<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix card-header">
        <span>文章分类</span>
        <el-button type="primary" size="small" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="cateList" border :header-cell-style="{ color: '#000' }">
        <el-table-column type="index" label="序号" width="100"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button size="mini" type="primary" @click="alterCategory(obj.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="deleteCategory(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" :before-close="resetAddForm">
      <el-form label-width="80px" :model="addData" :rules="addFormRules" ref="formRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改文章分类" :visible.sync="alterVisible" width="35%">
      <el-form label-width="80px" :model="alterData" :rules="addFormRules" ref="alterRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="alterData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="alterData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="alterVisible = false">取 消</el-button>
        <el-button type="primary" @click="alterArtCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addArtCateAPI, deleteCategoryAPI, alterCategoryAPI } from '@/APl/article.js'
export default {
  data() {
    return {
      cateList: [],
      addVisible: false,
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      addFormRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '长度在 1 到 10 位非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '长度在 1 到 15 位字母数字', trigger: 'blur' }
        ]
      },
      alterData: {
        cate_name: '',
        cate_alias: ''
      },
      alterVisible: false
    }
  },
  created() {
    this.initCategory()
  },
  methods: {
    async initCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.cateList = res.data
      } else {
        this.$message.error(res.message)
      }
    },
    addArtCate() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          const { data: res } = await addArtCateAPI(this.addData)
          if (res.code === 0) {
            this.initCategory()
            this.addVisible = false
            this.$refs.formRef.resetFields()
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
        }
      })
    },
    resetAddForm(done) {
      this.$refs.formRef.resetFields()
      this.addVisible = false
      // done()
    },
    deleteCategory(id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除前两个分类')
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deleteCategoryAPI(id)
          if (res.code === 0) {
            this.initCategory()
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(() => {})
    },
    alterCategory(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改前两个分类')
      this.alterVisible = true
      this.alterData = { ...row }
    },
    alterArtCate() {
      this.$refs.alterRef.validate(async valid => {
        if (valid) {
          const { data: res } = await alterCategoryAPI(this.alterData)
          if (res.code === 0) {
            this.$message.success(res.message)
            this.initCategory()
            this.alterVisible = false
          } else {
            this.$message.error(res.message)
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  font-size: 13px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
